package saxion.speelveld.spaceengineer.controller;

import java.util.ArrayList;

import saxion.speelveld.spaceengineer.R;
import saxion.speelveld.spaceengineer.model.Resource;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.SpinnerAdapter;
import android.widget.TextView;

public class simpleSpinnerProducingResourcesAdapter extends ArrayAdapter<Resource[]> implements SpinnerAdapter
{
	Context context;

	ArrayList<Resource[]> resourceListArray;

	Resource[] selectedResource;

	private ImageView ivSimpleResourceProductionGain;

	private TextView tvSimpleResourceProductionGain;

	private ImageView ivSimpleResourceProductionCost;

	private TextView tvSimpleResourceProductionCost;

	public simpleSpinnerProducingResourcesAdapter(Context context, ArrayList<Resource[]> resources)
	{
		super(context, R.layout.spinner_producing_resources_simple, resources);
		this.resourceListArray = resources;
		this.context = context;
		this.selectedResource = selectedResource;
	}

	@Override
	public View getDropDownView(int position, View convertView, ViewGroup parent)
	{
		if (convertView == null)
		{
			LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			convertView = inflater.inflate(R.layout.spinner_producing_resources_simple, parent, false);
		}

		Resource[] resourceArray = resourceListArray.get(position);
		fillView(convertView, resourceArray);

		return convertView;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent)
	{
		if (convertView == null)
		{
			LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			convertView = inflater.inflate(R.layout.spinner_producing_resources_simple, parent, false);
		}

		Resource[] resourceArray;

		// if (selectedResource == null)
		// {
		resourceArray = resourceListArray.get(position);
		// }
		// else
		// {
		// resourceArray = selectedResource;
		// }
		fillView(convertView, resourceArray);

		return convertView;

	}

	private View fillView(View viewToUse, Resource[] resourceArray)
	{

		ivSimpleResourceProductionGain = (ImageView) viewToUse.findViewById(R.id.ivSimpleResourceProductionGain);
		ivSimpleResourceProductionGain.setImageBitmap(BitmapManager.getBitmapByResourceType(
			resourceArray[0].getResourceType(), context));

		tvSimpleResourceProductionGain = (TextView) viewToUse.findViewById(R.id.tvSimpleResourceProductionGain);
		tvSimpleResourceProductionGain.setText(resourceArray[0].getAmount() + "");

		ivSimpleResourceProductionCost = (ImageView) viewToUse.findViewById(R.id.ivSimpleResourceProductionCost);
		ivSimpleResourceProductionCost.setImageBitmap(BitmapManager.getBitmapByResourceType(
			resourceArray[1].getResourceType(), context));

		tvSimpleResourceProductionCost = (TextView) viewToUse.findViewById(R.id.tvSimpleResourceProductionCost);
		tvSimpleResourceProductionCost.setText(resourceArray[1].getAmount() + "");

		return viewToUse;
	}
}
